package mumu.android.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Random;

import mumu.android.R;
import mumu.android.lib.tool.Tools;
import android.app.Activity;
import android.content.res.AssetManager.AssetInputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.widget.ImageView;

/**
 * 
 * bind服务处理。 关闭activity后，服务终止
 * 
 * @author Administrator
 * 
 */
public class fileEncry2 extends Activity {

	public static String OriginalFile = Tools.getSDPath() + "/hsy/unzipfile/1/Advertisement/1.png";
	public static String EncryFile = Tools.getSDPath() + "/hsy/unzipfile/1/Advertisement/1_en";
	// public static String OriginalFile = Tools.getSDPath() +
	// "/hsy/zipfile/1/Advertisement.zip";
	// public static String EncryFile = Tools.getSDPath() +
	// "/hsy/zipfile/1/Advertisement/aAdvertisement.zip";
	public static String descryFile = Tools.getSDPath() + "/hsy/unzipfile/1/Advertisement/1_des.png";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fileencry);
		encry();
		Bitmap bm = decry();
		ImageView img_1 = (ImageView) findViewById(R.id.img_1);
		if (bm != null)
			img_1.setImageBitmap(bm);

	}

	public Bitmap decry() {
		FileInputStream ofis = null;
		try {
			File file = new File(EncryFile);
			ofis = new FileInputStream(file);
			byte[] skipByteCount = new byte[1];
			ofis.read(skipByteCount);
			int count = skipByteCount[0];
			ofis.skip(count);
			return BitmapFactory.decodeStream(ofis);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ofis != null)
					ofis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public void encry() {
		FileInputStream ofis = null;
		FileOutputStream efos = null;
		try {
			File efile = new File(EncryFile);
			if (!efile.exists()) {
				efile.createNewFile();
			} else {
				efile.delete();
			}
			File ofile = new File(OriginalFile);
			ofis = new FileInputStream(ofile);
			byte[] buffer = new byte[1024];
			int l = 0;
			efos = new FileOutputStream(efile);
			byte skpCount = 10;
			byte[] prebufer = new byte[skpCount];
			efos.write(skpCount);
			efos.write(prebufer);
			while ((l = ofis.read(buffer)) >= 0) {
				efos.write(buffer);
			}
			// ofos.write(prebufer);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ofis != null)
					ofis.close();
				if (efos != null)
					efos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void ran() {
		File file = new File(descryFile);
		File efile = new File(EncryFile);
		FileOutputStream efos = null;
		try {
			RandomAccessFile randomAccessFile = new RandomAccessFile(efile, "rw");
			randomAccessFile.skipBytes(11);
			byte[] buffer = new byte[1024];
			int l = 0;
			efos = new FileOutputStream(file);
			while ((l = randomAccessFile.read(buffer)) >= 0) {
				efos.write(buffer);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (efos != null)
				try {
					efos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}

	}

	public static Bitmap decodeFile(String pathName, Options opts) {
		Bitmap bm = null;
		InputStream stream = null;
		try {
			stream = new FileInputStream(pathName);
			bm = BitmapFactory.decodeStream(stream, null, opts);
		} catch (Exception e) {
			/*
			 * do nothing. If the exception happened on open, bm will be null.
			 */
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException e) {
					// do nothing here
				}
			}
		}
		return bm;
	}

	public static Bitmap decodeSampledBitmapFromFile(String filename, Bitmap.Config config) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		options.inPurgeable = true;
		options.inInputShareable = true;
		BitmapFactory.decodeFile(filename, options);
		options.inSampleSize = 1;
		options.inJustDecodeBounds = false;
		if (config != null) {
			options.inPreferredConfig = config;
		}
		try {
			return BitmapFactory.decodeFile(filename, options);
		} catch (Throwable e) {

			return null;
		}
	}

}
